package com.hs.mod_syslog

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.hs.framework.base.BaseDataBindFragment
import com.hs.framework.view.PaginationRecyclerView
import com.hs.framework.view.SharedHeaderView
import com.hs.mod_syslog.adapter.SysLogListAdapter
import com.hs.mod_syslog.databinding.SysLogFragmentListBinding
import com.hs.mod_syslog.viewmodel.SysLogViewModel
import com.hs.room.model.sys.SysLog
import dagger.hilt.android.AndroidEntryPoint

/**
 * 日志的列表页面
 */
@AndroidEntryPoint
class SysLogListFragment : BaseDataBindFragment<SysLogFragmentListBinding>() {

    lateinit var sysLogViewModel: SysLogViewModel;

    var adapter = SysLogListAdapter()

    override fun initView(view: View, savedInstanceState: Bundle?) {

        sysLogViewModel = ViewModelProvider(this).get(SysLogViewModel::class.java)

        mBinding!!.mPaginationRecyclerView.setAdapter(adapter)

        mBinding!!.mPaginationRecyclerView.setTotal(0)

        observeData()

        sysLogViewModel.getByCombinedConditions(1,10);

        sysLogViewModel.getAllCountByCombinedConditions()

        mBinding!!.mPaginationRecyclerView.setPaginationRecyclerViewCallback(object : PaginationRecyclerView.PaginationRecyclerViewCallback<SysLog>{
            override fun refreshData(index: Int) {
                println("refreshData: ${index}")

                sysLogViewModel.getByCombinedConditions(index,10);

                sysLogViewModel.getAllCountByCombinedConditions()

            }

            override fun onClickItem(item: SysLog) {
//                findNavController(view).navigateUp()

            }

        })

        mBinding!!.mSharedHeaderView.setHeaderTitle("系统日志")

        mBinding!!.mSharedHeaderView.setCallBack(object : SharedHeaderView.CallBack{
            override fun onFinish() {
                findNavController().navigateUp()
            }

        })
    }

   private fun observeData(){

        sysLogViewModel.totalLiveData.observe(this){
            mBinding!!.mPaginationRecyclerView.setTotal(it)
        }

        sysLogViewModel.sysListData.observe(this){
            println("${it}")
            adapter.setDataList(it)
        }

    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding?.mPaginationRecyclerView?.destroyView()
        mBinding = null
    }
}